﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Asah.SiteWorx.ProductCatalog;
using System.Web.UI.WebControls.WebParts;
using System.Configuration;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;

public partial class SiteWorx_WebParts_Advanced_ProductCatalog_ProductCatalog : BaseWebPart
{
    private int _productCatalogId = 0;
    private int _repeatColumns = 2;
    private int _thumbnailSize = 0;
    private int _maxThumbnailHeight = 0;

    [Personalizable(PersonalizationScope.Shared)]
    public int ProductCatalogId
    {
        get
        {
            return _productCatalogId;
        }
        set
        {
            _productCatalogId = value;
        }
    }

    [Personalizable(PersonalizationScope.Shared)]
    public int RepeatColumns
    {
        get
        {
            return _repeatColumns;
        }
        set
        {
            _repeatColumns = value;
        }
    }

    [Personalizable(PersonalizationScope.Shared)]
    public int ThumbnailSize
    {
        get
        {
            if (_thumbnailSize <= 0)
                if (ConfigurationManager.AppSettings["ProductCatalogThumbDefaultSize"] != null && 
                    ConfigurationManager.AppSettings["ProductCatalogThumbDefaultSize"].ToString().Length > 0)
                    _thumbnailSize = int.Parse(ConfigurationManager.AppSettings["ProductCatalogThumbDefaultSize"]);
            return _thumbnailSize;
        }
        set
        {
            _thumbnailSize = value;
        }
    }

    [Personalizable(PersonalizationScope.Shared)]
    public int MaxThumbnailHeight
    {
        get
        {
            if (_maxThumbnailHeight <= 0)
                if (ConfigurationManager.AppSettings["ProductCatalogThumbMaxHeight"] != null && 
                    ConfigurationManager.AppSettings["ProductCatalogThumbMaxHeight"].ToString().Length > 0)
                    _maxThumbnailHeight = int.Parse(ConfigurationManager.AppSettings["ProductCatalogThumbMaxHeight"]);
            return _maxThumbnailHeight;
        }
        set
        {
            _maxThumbnailHeight = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //pnlProductDetails.Visible = false;
        if (!Page.IsPostBack)
        {
            BindProductCatalog();
            pnlEditContentButton.Visible = Page.User.IsInRole("Administrator") && !Utility.PageInPreviewMode();
        }
    }

    private void BindProductCatalog()
    {
        if (ProductCatalogId > 0)
        {
            dlstProducts.Visible = true;
            //pnlProductDetails.Visible = false;
            dlstProducts.RepeatColumns = RepeatColumns;
            dlstProducts.DataSource = Asah.SiteWorx.ProductCatalog.Catalog.GetCatalog(ProductCatalogId).Products;
            dlstProducts.DataBind();
        }
        else
        {
            dlstProducts.Visible = false;
            //pnlProductDetails.Visible = false;
        }
    }
    
    protected void dlstProducts_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Select")
            Response.Redirect("~/ProductDetail.aspx?id=" + e.CommandArgument);
    }

    protected void lnkReturn_Click(object sender, EventArgs e)
    {
        //pnlProductDetails.Visible = false;
        dlstProducts.Visible = true;
    }

    protected void dlstProducts_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item || 
            e.Item.ItemType == ListItemType.SelectedItem)
             ((HtmlGenericControl)e.Item.FindControl("productImg")).Attributes.Add("style", "vertical-align: middle;text-align: center;height: " + 
                 MaxThumbnailHeight +"px;");

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        RemoveWebPart();
    }

    protected override void RemoveWebPart()
    {
        base.RemoveWebPart();
    }

    protected void lnkbtnUpdateProductCatalogWPData_Click(object sender, EventArgs e)
    {
        string productCatalogWPDataId = hidProductCatalogWPDataId.Value;
        ProductCatalogWebPartData data = (ProductCatalogWebPartData)Session[productCatalogWPDataId];
        this.MaxThumbnailHeight = data.MaxThumbnailHeight;
        this.ProductCatalogId = data.ProductCatalogId;
        this.RepeatColumns = data.RepeatColumns;
        this.ThumbnailSize = data.ThumbnailSize;
        
        //clear the content id and session variable
        hidProductCatalogWPDataId.Value = "";
        Session[productCatalogWPDataId] = null;

        BindProductCatalog();
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string productCatalogWPDataId = Guid.NewGuid().ToString() + "_ProductCatalog";
        Session[productCatalogWPDataId] = new ProductCatalogWebPartData(this.ProductCatalogId, this.RepeatColumns,
                                                                    this.ThumbnailSize, this.MaxThumbnailHeight);
        hidProductCatalogWPDataId.Value = productCatalogWPDataId;

        Telerik.Web.UI.RadWindow productCatalogEditor = new Telerik.Web.UI.RadWindow();
        productCatalogEditor.NavigateUrl = "~/SiteWorx/WebParts/Advanced/ProductCatalog/ProductCatalogEditor.aspx?productCatalogWPDataId=" + productCatalogWPDataId;
        productCatalogEditor.VisibleOnPageLoad = true;
        productCatalogEditor.Modal = true;
        productCatalogEditor.Behaviors = WindowBehaviors.Move | WindowBehaviors.Resize;
        productCatalogEditor.ShowContentDuringLoad = false;
        productCatalogEditor.VisibleStatusbar = false;
        productCatalogEditor.Width = Unit.Pixel(585);
        productCatalogEditor.Height = Unit.Pixel(300);

        pnlEditContentButton.Controls.Add(productCatalogEditor);

        BuildUpdateScript();
    }

    private void BuildUpdateScript()
    {
        string productCatalogWPDataId = hidProductCatalogWPDataId.Value;
        if (productCatalogWPDataId.Length > 0)
        {
            string functionName = "Update_" + productCatalogWPDataId.Replace("-", "_");
            string script = "function " + functionName + "(sender){__doPostBack('" + lnkbtnUpdateProductCatalogWPData.UniqueID + "','')}";
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), functionName, script, true);
        }
    }
}